package cn.edu.nju.software.android.adapter;

import java.util.ArrayList;

import me.maxwin.view.IListviewCallback;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.nju.software.android.activity.DetailedWeiboActivity;
import cn.edu.nju.software.android.activity.MainActivity;
import cn.edu.nju.software.android.activity.R;
import cn.edu.nju.software.android.model.Comment;
import cn.edu.nju.software.android.model.Status;
import cn.edu.nju.software.android.util.AsyncImageLoader;
import cn.edu.nju.software.android.util.ImageUtil;

@SuppressLint("HandlerLeak")
public class MessageListViewAdapter extends ArrayAdapter<Comment>{
	private Context context;
	private IListviewCallback callback;
	private ArrayList<Comment> comments = new ArrayList<Comment>();

	private int type ;
	public static final int AT_ME = 1;
	public static final int COMMENT_ME = 2;
	
	private Handler failureHandler = new Handler(){
		public void handleMessage(Message msg) {
			Toast.makeText(context, "WIFI没有打开", Toast.LENGTH_SHORT).show();
		}
	};
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			notifyDataSetChanged(); //发送消息通知ListView更新
			Bundle data = msg.getData();
			callback.onComplete();
			callback.enableLoadFunction();
			if(data.getInt("type") == IListviewCallback.LOAD_MORE){
				int newNum = msg.getData().getInt("num");
				if(newNum == 0)
					callback.disableLoadFunction();
				else{
					callback.enableLoadFunction();
				}
			}else if(data.getInt("type") == IListviewCallback.REFRESH){
			}
		}
	};

	public MessageListViewAdapter(Context context,IListviewCallback callback, int type) {
		super(context, R.layout.layout_messages);
		this.context = context;
		this.callback = callback;
		this.type = type;
		getNewestComments();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = inflater.inflate(R.layout.listview_message, parent, false);

		ImageButton userImageButton = (ImageButton)convertView.findViewById(R.id.message_head);
		Button userNameButton = (Button)convertView.findViewById(R.id.message_name);
		TextView messageTextView = (TextView)convertView.findViewById(R.id.messsage_content);
		TextView messagedTextView = (TextView)convertView.findViewById(R.id.messaged_content);

		setListViewListener(convertView);

		AsyncImageLoader userImageAsyLoader = new AsyncImageLoader();

		final Comment comment = comments.get(position);

		Drawable userCachedImage = userImageAsyLoader.loadDrawable(context, failureHandler, comment
				.getUser().getProfileImageUrl(), userImageButton,
				new AsyncImageLoader.ImageCallback() {
			@Override
			public void imageLoaded(Drawable imageDrawable,
					ImageView imageView, String imageUrl) {
				if(imageDrawable != null)
					imageView.setImageDrawable(ImageUtil.resizeProfilePic(imageDrawable));
			}
		},100, 100);
		if (userCachedImage == null) {
			userImageButton.setImageResource(R.drawable.user);
		} else {
			userImageButton.setImageDrawable(ImageUtil.resizeProfilePic(userCachedImage));
		}		

		userNameButton.setText(comment.getUser().getName());
		if(comment.getReply_comment() == null){
			messagedTextView.setText("回复我的微博:" + comment.getStatus().getText());
		}else{
			messagedTextView.setText("回复我的评论:" + comment.getReply_comment().getText());
		}
		messageTextView.setText(comment.getText());

		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, DetailedWeiboActivity.class);
				intent.putExtra("status", comment.getStatus());
				intent.putExtra("parent", MainActivity.class);
				context.startActivity(intent);
			}

		});
		return convertView;
	}

	@Override
	public int getCount() {
		return comments.size();
	}

	@Override
	public Comment getItem(int position) {
		return comments.get(position);
	}

	public Status getStatus(int position){
		return comments.get(position).getStatus();
	}

	public void getNewestComments(){
		long since_id = 0;
		switch(type){
		case AT_ME:
			if (comments.size() != 0)
				since_id = comments.get(0).getId();
			Comment.downloadNewestMentionMeComments(since_id, comments, handler);
			break;
		case COMMENT_ME:
			if (comments.size() != 0)
				since_id = comments.get(0).getId();
			Comment.downloadNewestToMeComments(since_id, comments, handler);
			break;
		}
	}

	public void getMoreComments(){
		long max_id = 0;
		switch(type){
		case AT_ME:
			if (comments.size() != 0)
				max_id = comments.get(comments.size()-1).getId();
			Comment.downloadMoreMentionsMeComments(max_id, comments, handler);
			break;
		case COMMENT_ME:
			if (comments.size() != 0)
				max_id = comments.get(comments.size()-1).getId();
			Comment.downloadMoreToMeComments(max_id, comments, handler);
			break;
		}
	}

	private void setListViewListener(View view) {
		view.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					int touch = context.getResources().getColor(R.color.touch);
					v.setBackgroundColor(touch);
					v.findViewById(R.id.message_head).setBackgroundColor(touch);
					v.findViewById(R.id.message_layout).setBackgroundColor(touch);
					v.findViewById(R.id.messaged_layout).setBackgroundColor(touch);
					v.findViewById(R.id.message_name).setBackgroundColor(touch);
				} else {
					int white = context.getResources().getColor(R.color.white);
					v.setBackgroundColor(white);
					v.findViewById(R.id.message_head).setBackgroundColor(white);
					v.findViewById(R.id.message_layout).setBackgroundColor(white);
					v.findViewById(R.id.messaged_layout).setBackgroundColor(white);
					v.findViewById(R.id.message_name).setBackgroundColor(white);
				}
				return false;
			}
		});
	}

}
